<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="typescript,react,mobx,">





  <link rel="alternate" href="/blog/atom.xml" title="编程那点事" type="application/atom+xml">






<meta name="description" content="为什么用TypeScriptTypeScript为JavaScript这个弱类型语言添加了基本的类型检测以及对于ES6甚至ES7的一些高级语法的转移，在一定程度上相当于增强功能版的babel，现在的很多开源项目为了得到更好的项目管理和维护性能，使用了TypeScript或者Flow来为JavaScript添加强类型。TypeScript添加了很多强类型语言的功能，比如interface，class">
<meta name="keywords" content="typescript,react,mobx">
<meta property="og:type" content="article">
<meta property="og:title" content="typescript+react+mobx的todo实例教程">
<meta property="og:url" content="https://lhweb.gitee.io/blog/2018/09/23/typescript-react-mobx的todo实例教程/index.html">
<meta property="og:site_name" content="编程那点事">
<meta property="og:description" content="为什么用TypeScriptTypeScript为JavaScript这个弱类型语言添加了基本的类型检测以及对于ES6甚至ES7的一些高级语法的转移，在一定程度上相当于增强功能版的babel，现在的很多开源项目为了得到更好的项目管理和维护性能，使用了TypeScript或者Flow来为JavaScript添加强类型。TypeScript添加了很多强类型语言的功能，比如interface，class">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2019-10-03T05:53:45.934Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="typescript+react+mobx的todo实例教程">
<meta name="twitter:description" content="为什么用TypeScriptTypeScript为JavaScript这个弱类型语言添加了基本的类型检测以及对于ES6甚至ES7的一些高级语法的转移，在一定程度上相当于增强功能版的babel，现在的很多开源项目为了得到更好的项目管理和维护性能，使用了TypeScript或者Flow来为JavaScript添加强类型。TypeScript添加了很多强类型语言的功能，比如interface，class">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://lhweb.gitee.io/blog/2018/09/23/typescript-react-mobx的todo实例教程/">





  <title>typescript+react+mobx的todo实例教程 | 编程那点事</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">编程那点事</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://lhweb.gitee.io/blog/blog/2018/09/23/typescript-react-mobx的todo实例教程/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="lhweb">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="编程那点事">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">typescript+react+mobx的todo实例教程</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-09-23T09:13:08+08:00">
                2018-09-23
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/typescript/" itemprop="url" rel="index">
                    <span itemprop="name">typescript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="为什么用TypeScript"><a href="#为什么用TypeScript" class="headerlink" title="为什么用TypeScript"></a>为什么用TypeScript</h1><p>TypeScript为JavaScript这个弱类型语言添加了基本的类型检测以及对于ES6甚至ES7的一些高级语法的转移，在一定程度上相当于增强功能版的babel，现在的很多开源项目为了得到更好的项目管理和维护性能，使用了TypeScript或者Flow来为JavaScript添加强类型。<br>TypeScript添加了很多强类型语言的功能，比如interface，class(ES6已有，功能增强)，类型检测，泛型等。<br>TypeScript的入门可以看一个<a href="https://www.tslang.cn/docs/handbook/typescript-in-5-minutes.html" target="_blank" rel="noopener">TypeScript中文官方文档</a>。</p>
<h1 id="项目环境构建"><a href="#项目环境构建" class="headerlink" title="项目环境构建"></a>项目环境构建</h1><p>这里默认大家的nodeJS基础。</p>
<p>这里项目环境的构建使用官方提供的create-react-app脚手架工具和微软提供的TypeScript支持TypeScript-react-starter，这两个项目都可以在github上面找到。</p>
<h2 id="构建基本环境"><a href="#构建基本环境" class="headerlink" title="构建基本环境"></a>构建基本环境</h2><figure class="highlight dsconfig"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">cnpm </span><span class="string">install </span>-g <span class="built_in">create-react-app</span></span><br><span class="line"><span class="built_in">create-react-app</span> <span class="string">my-app </span><span class="built_in">--scripts-version=react-scripts-ts</span></span><br><span class="line"><span class="built_in">#</span> 如果感觉下载速度比较慢的话，请切换<span class="string">npm的</span>镜像源到淘宝的镜像源</span><br><span class="line"><span class="string">npm </span><span class="string">config </span><span class="string">set </span><span class="string">registry </span><span class="string">https:</span>//<span class="string">registry.</span><span class="string">npm.</span><span class="string">taobao.</span><span class="string">org</span></span><br><span class="line"><span class="string">#</span> 可以检查一下镜像源是否切换成功</span><br><span class="line"><span class="string">npm </span><span class="string">config </span><span class="string">get </span><span class="string">registry</span></span><br></pre></td></tr></table></figure>
<p>基本上已经获得了一个完整的React项目，这里的webpack以及各种配置默认是不能够修改的，如果希望修改的话需要将配置文件弹出。</p>
<figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># yarn</span></span><br><span class="line">yarn eject</span><br><span class="line">yarn start</span><br><span class="line"><span class="comment"># npm</span></span><br><span class="line"><span class="built_in">npm</span> run eject</span><br><span class="line"><span class="built_in">npm</span> start</span><br></pre></td></tr></table></figure>
<p><strong>注意：eject这个命令是不可逆的。</strong><br>这里用yarn或者是npm都可以，目前感觉使用yarn会方便一些。</p>
<h2 id="增加额外的配置"><a href="#增加额外的配置" class="headerlink" title="增加额外的配置"></a>增加额外的配置</h2><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn <span class="keyword">add</span><span class="bash"> mobx mobx-react</span></span><br></pre></td></tr></table></figure>
<p>ps:@types 命令在windows powershell会直接报错，如果是windows环境下，cmd来执行命令。<br>这里要注意，一定要安装所有外部依赖模块的类型定义，否则模块在使用的时候会因为没有TypeScript的类型定义而报错。</p>
<p>使用typescript开发react，对于所有需要的外部模块，都需要有.d.ts的类型定义文件，一般比较常用的模块在npm上面都是有已经写好的类型定义文件了。但是一些比较小的外部依赖还是需要自己去写。</p>
<p>类型定义文件一般放在/node_modules/@types/*目录下面，类型名为.d.ts，关于类型定义文件具体可以看TypeScript的模块部分相关文档。</p>
<h1 id="实现TodoList"><a href="#实现TodoList" class="headerlink" title="实现TodoList"></a>实现TodoList</h1><h2 id="实现功能"><a href="#实现功能" class="headerlink" title="实现功能"></a>实现功能</h2><ul>
<li>Todo条目的列表展示</li>
<li>增加Todo条目</li>
<li>修改完成状态</li>
<li>删除Todo条目</li>
</ul>
<h3 id="完整示例"><a href="#完整示例" class="headerlink" title="完整示例"></a>完整示例</h3><p>打开App.tsx编辑</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> React from <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; observable,action,computed &#125; from <span class="string">"mobx"</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;observer&#125; from <span class="string">"mobx-react"</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">'./App.css'</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Todo</span> </span>&#123;</span><br><span class="line">    id = Math.random(); <span class="comment">//这里为只读属性，不用@observable修饰。</span></span><br><span class="line">    <span class="meta">@observable</span> title:string = <span class="string">''</span>;</span><br><span class="line">    <span class="meta">@observable</span> finished:boolean = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">constructor</span>(title:string)&#123;</span><br><span class="line">      <span class="keyword">this</span>.title = title;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@action</span>.bound toggle()&#123;</span><br><span class="line">      <span class="keyword">this</span>.finished = !<span class="keyword">this</span>.finished;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* @action.bound 解决this作用域的问题 */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Store</span> </span>&#123;</span><br><span class="line"> <span class="meta">@observable</span> todos:Array&lt;any&gt; = [];</span><br><span class="line"> <span class="meta">@action</span>.bound createTodo(title:string)&#123;</span><br><span class="line">    <span class="keyword">this</span>.todos.unshift(new Todo(title));</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="meta">@computed</span> <span class="keyword">get</span> left()&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="keyword">this</span>.todos.filter(todo=&gt;!todo.finished).length</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="meta">@action</span>.bound removeTodo(todo:any)&#123;</span><br><span class="line">    <span class="keyword">this</span>.todos.remove(todo); <span class="comment">//这里可以用传todo.id来解决问题，</span></span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*这里要做一个特别的说明，这里删除todo如何跟后端交互，后端交互有2种形式，一种是根据传ID，根据ID来删除记录。那么这种传todo过来删除的方式就不太友好。还有一种就是提交新的todos的数据到后端处理。</span></span><br><span class="line"><span class="comment">如果数据量大的话，那么肯定是传ID来删除记录会比较合适。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">var</span> store = new Store();</span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">TodoItemProps</span></span>&#123;</span><br><span class="line">   todo:Todo</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* React.Fragment也可以用数组来组织替代，但是需要有key属性。 */</span></span><br><span class="line"><span class="meta">@observer</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TodoItem</span> <span class="title">extends</span> <span class="title">React</span>.<span class="title">Component</span>&lt;<span class="type">TodoItemProps,&#123;&#125;</span>&gt;</span>&#123;</span><br><span class="line">   <span class="keyword">constructor</span>(props:TodoItemProps)&#123;</span><br><span class="line">     <span class="keyword">super</span>(props);</span><br><span class="line">   &#125;</span><br><span class="line">   handleChange = (e:any)=&gt;&#123;</span><br><span class="line">      <span class="keyword">this</span>.props.todo.toggle(); </span><br><span class="line">   &#125;</span><br><span class="line">   render()&#123;</span><br><span class="line">     <span class="keyword">const</span> todo = <span class="keyword">this</span>.props.todo;</span><br><span class="line">     <span class="keyword">return</span>(</span><br><span class="line">       &lt;React.Fragment&gt;</span><br><span class="line">       &lt;input type=<span class="string">"checkbox"</span> className=<span class="string">"toggle"</span> checked=&#123;todo.finished&#125; onChange=&#123;<span class="keyword">this</span>.handleChange&#125;/&gt;</span><br><span class="line">       &lt;span className=&#123;[<span class="string">'title'</span>,todo.finished&amp;&amp;<span class="string">'finished'</span>].join(<span class="string">' '</span>)&#125;&gt;&#123;todo.title&#125;&lt;/span&gt;</span><br><span class="line">       &lt;/React.Fragment&gt;</span><br><span class="line">     )</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">TodoListProps</span></span>&#123;</span><br><span class="line">   store:Store</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">TodoListState</span></span>&#123;</span><br><span class="line">  inputValue:string</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">@observer</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TodoList</span> <span class="title">extends</span> <span class="title">React</span>.<span class="title">Component</span>&lt;<span class="type">TodoListProps,TodoListState</span>&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span>(props:TodoListProps)&#123;</span><br><span class="line">    <span class="keyword">super</span>(props);</span><br><span class="line">    <span class="keyword">this</span>.state = &#123;</span><br><span class="line">      inputValue:<span class="string">''</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">   </span><br><span class="line">  handleSubmit = (e:any)=&gt;&#123;</span><br><span class="line">    e.preventDefault() <span class="comment">//阻止整个页面被提交</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> store = <span class="keyword">this</span>.props.store;</span><br><span class="line">    <span class="keyword">var</span> inputValue = <span class="keyword">this</span>.state.inputValue;</span><br><span class="line">    store.createTodo(inputValue);</span><br><span class="line">    <span class="keyword">this</span>.setState(&#123;</span><br><span class="line">      inputValue:<span class="string">''</span></span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">  handleChange = (e:any)=&gt;&#123;</span><br><span class="line">    <span class="keyword">var</span> inputValue = e.target.value; <span class="comment">//通过target.value获取input的值</span></span><br><span class="line">    <span class="keyword">this</span>.setState(</span><br><span class="line">      &#123;</span><br><span class="line">        inputValue</span><br><span class="line">      &#125;</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">   render()&#123;</span><br><span class="line">     <span class="keyword">const</span> store = <span class="keyword">this</span>.props.store;</span><br><span class="line">     <span class="keyword">const</span> todos = store.todos;</span><br><span class="line"></span><br><span class="line">     <span class="keyword">return</span>(</span><br><span class="line">       </span><br><span class="line">         &lt;div className=<span class="string">"todo-list"</span>&gt;</span><br><span class="line">         &lt;header&gt;</span><br><span class="line">           &lt;form onSubmit=&#123;<span class="keyword">this</span>.handleSubmit&#125;&gt;</span><br><span class="line">            &lt;input type=<span class="string">"text"</span> onChange=&#123;<span class="keyword">this</span>.handleChange&#125; value=&#123;<span class="keyword">this</span>.state.inputValue&#125; className=<span class="string">"input"</span> placeholder=<span class="string">"What needs to be finished?"</span>/&gt;</span><br><span class="line">           &lt;/form&gt;</span><br><span class="line">         &lt;/header&gt;</span><br><span class="line">         &lt;ul&gt;</span><br><span class="line">           &#123;todos.map(todo=&gt;&#123;</span><br><span class="line">             <span class="keyword">return</span> &lt;li key=&#123;todo.id&#125; className=<span class="string">"todo-item"</span>&gt;</span><br><span class="line">             &lt;TodoItem todo=&#123;todo&#125;/&gt;</span><br><span class="line">             &lt;span className=<span class="string">"delete"</span> onClick=&#123;e=&gt;store.removeTodo(todo)&#125;&gt;X&lt;/span&gt;</span><br><span class="line">             &lt;/li&gt;;</span><br><span class="line">           &#125;)&#125;</span><br><span class="line">         &lt;/ul&gt;</span><br><span class="line">         &lt;footer&gt;</span><br><span class="line">         &#123;store.left&#125; item(s) unfinished</span><br><span class="line">         &lt;/footer&gt;</span><br><span class="line">         &lt;/div&gt;</span><br><span class="line">      </span><br><span class="line">     )</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">App</span> <span class="title">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">  <span class="keyword">public</span> render() &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      &lt;div className=<span class="string">"App"</span>&gt;</span><br><span class="line">        &lt;TodoList store = &#123;store&#125; /&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure>
<p>如何通过传ID来处理todolist数据？我们可以给Array数组的原型链来创建删除方法。示例代码如下：<br><figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*简单版*/</span></span><br><span class="line"><span class="built_in">Array</span>.prototype.remove = <span class="function"><span class="keyword">function</span>(<span class="params">key,val</span>)</span>&#123;</span><br><span class="line"> </span><br><span class="line">   <span class="keyword">for</span>(<span class="keyword">let</span> i=<span class="number">0</span>;i&lt;arr.length;i++)</span><br><span class="line">  &#123;</span><br><span class="line">     <span class="keyword">if</span>(<span class="keyword">this</span>[i][key] == val)</span><br><span class="line">     &#123;</span><br><span class="line">       <span class="keyword">this</span>.splice(i,<span class="number">1</span>)</span><br><span class="line">     &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr = [&#123;<span class="attribute">id:</span><span class="string">0,name</span>:<span class="string">'香蕉0'</span>&#125;,&#123;<span class="attribute">id:</span><span class="string">1,name</span>:<span class="string">'香蕉1'</span>&#125;,&#123;<span class="attribute">id:</span><span class="string">2,name</span>:<span class="string">'香蕉2'</span>&#125;,&#123;<span class="attribute">id:</span><span class="string">3,name</span>:<span class="string">'香蕉3'</span>&#125;]</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(arr.remove(<span class="string">'id'</span>,<span class="number">0</span>)) </span><br><span class="line"><span class="built_in">console</span>.log(arr.remove(<span class="string">'name'</span>,<span class="number">3</span>)) </span><br><span class="line"><span class="comment">/*动身尝试，看看会得到什么结果*/</span></span><br></pre></td></tr></table></figure></p>
<p>通过此方法来删除数据，我们就可以传ID到后端来交互数据。</p>
<p>打开App.css编辑,增加以下样式</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.input</span> &#123;</span><br><span class="line">  <span class="attribute">padding</span>:<span class="number">15px</span>;</span><br><span class="line">  <span class="attribute">border</span>:<span class="number">1px</span> solid <span class="number">#ccc</span>;</span><br><span class="line">  <span class="attribute">font-size</span>: <span class="number">24px</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">400px</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.todo-item</span>&#123;</span><br><span class="line">  <span class="attribute">display</span>: flex;</span><br><span class="line">  <span class="attribute">align-items</span>: center;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">40px</span>;</span><br><span class="line">  <span class="attribute">border-bottom</span>:<span class="number">1px</span> solid <span class="number">#ccc</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.todo-item</span> <span class="selector-class">.toggle</span>&#123;</span><br><span class="line">  <span class="attribute">margin-right</span>: <span class="number">20px</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.todo-item</span> <span class="selector-class">.title</span>&#123;</span><br><span class="line">  <span class="attribute">font-size</span>:<span class="number">24px</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#000</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.todo-item</span> <span class="selector-class">.title</span><span class="selector-class">.finished</span>&#123;</span><br><span class="line">  <span class="attribute">text-decoration</span>: line-through;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#ccc</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.todo-item</span> <span class="selector-class">.delete</span> &#123;</span><br><span class="line">  <span class="attribute">margin-left</span>:<span class="number">20px</span>;</span><br><span class="line">  <span class="attribute">cursor</span>: pointer;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>那么我们的TodoList就完成了，但是性能上还有优化的空间。</p>
<h1 id="最后说明"><a href="#最后说明" class="headerlink" title="最后说明"></a>最后说明</h1><p>本实例可能会出现报错现象。请用下面的脚手架来搭建环境。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">npm <span class="keyword">install</span> -g rwt-cli</span><br><span class="line">rwt init &lt;<span class="keyword">name</span>&gt;</span><br><span class="line">cd &lt;<span class="keyword">name</span>&gt;</span><br><span class="line">npm <span class="keyword">install</span></span><br><span class="line">npm <span class="keyword">start</span></span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/typescript/" rel="tag"># typescript</a>
          
            <a href="/blog/tags/react/" rel="tag"># react</a>
          
            <a href="/blog/tags/mobx/" rel="tag"># mobx</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/09/23/mobx介绍/" rel="next" title="mobx介绍">
                <i class="fa fa-chevron-left"></i> mobx介绍
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/09/23/TypeScript-不止稳，而且快/" rel="prev" title="TypeScript - 不止稳，而且快">
                TypeScript - 不止稳，而且快 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">lhweb</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">39</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#为什么用TypeScript"><span class="nav-number">1.</span> <span class="nav-text">为什么用TypeScript</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#项目环境构建"><span class="nav-number">2.</span> <span class="nav-text">项目环境构建</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#构建基本环境"><span class="nav-number">2.1.</span> <span class="nav-text">构建基本环境</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#增加额外的配置"><span class="nav-number">2.2.</span> <span class="nav-text">增加额外的配置</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#实现TodoList"><span class="nav-number">3.</span> <span class="nav-text">实现TodoList</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#实现功能"><span class="nav-number">3.1.</span> <span class="nav-text">实现功能</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#完整示例"><span class="nav-number">3.1.1.</span> <span class="nav-text">完整示例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#最后说明"><span class="nav-number">4.</span> <span class="nav-text">最后说明</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lhweb</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
